home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / ext2_200.zip / EXT2_SRC.ZIP / 32BITS / EXT2-OS2 / FSD32 / FS32_WRI.C < prev    next >
C/C++ Source or Header  |  1996-09-21  |  7KB  |  156 lines

  1. //
  2. // $Header: D:/32bits/ext2-os2/fsd32/RCS/fs32_write.c,v 1.1 1996/09/21 22:25:48 Willm Exp Willm $
  3. //
  4.  
  5. // 32 bits Linux ext2 file system driver for OS/2 WARP - Allows OS/2 to
  6. // access your Linux ext2fs partitions as normal drive letters.
  7. // Copyright (C) 1995, 1996 Matthieu WILLM
  8. //
  9. // This program is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation; either version 2 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. #ifdef __IBMC__
  23. #pragma strings(readonly)
  24. #endif
  25.  
  26.  
  27. #define INCL_DOS
  28. #define INCL_DOSERRORS
  29. #define INCL_NOPMAPI
  30. #include <os2.h>
  31.  
  32. #include <os2/types.h>
  33. #include <os2/StackToFlat.h>
  34. #include <linux/fs.h>
  35. #include <os2/os2proto.h>
  36. #include <os2/fsd32.h>
  37. #include <os2/DevHlp32.h>
  38. #include <os2/log.h>
  39. #include <os2/trace.h>
  40. #include <os2/errors.h>
  41. #include <os2/files.h>
  42. #include <os2/volume.h>
  43. #include <linux/fs_proto.h>
  44. #include <linux/stat.h>
  45. #include <os2/filefind.h>
  46.  
  47. /*
  48.  * struct fs32_write_parms {
  49.  *     unsigned short IOflag;
  50.  *     PTR16          pLen;
  51.  *     PTR16          pData;
  52.  *     PTR16          psffsd;
  53.  *     PTR16          psffsi;
  54.  * };
  55.  */
  56. int FS32ENTRY fs32_write(struct fs32_write_parms *parms) {
  57.     char           *pData;
  58.     struct sffsi32 *psffsi;
  59.     union  sffsd32 *psffsd;
  60.     int             rc;
  61.     unsigned short *pLen;
  62.     int     rc2, err;
  63.     unsigned long BytesWritten;
  64.     char          lock[12];
  65.     unsigned long PgCount;
  66.  
  67.  
  68.     parms = __StackToFlat(parms);
  69.  
  70.     if ((rc = DevHlp32_VirtToLin(parms->psffsi, __StackToFlat(&psffsi))) == NO_ERROR) {
  71.         if ((rc = DevHlp32_VirtToLin(parms->psffsd, __StackToFlat(&psffsd))) == NO_ERROR) {
  72.             if ((rc = DevHlp32_VirtToLin(parms->pData, __StackToFlat(&pData))) == NO_ERROR) {
  73.                 if ((rc = DevHlp32_VirtToLin(parms->pLen, __StackToFlat(&pLen))) == NO_ERROR) {
  74.  
  75.                     if (trace_FS_WRITE) {
  76.                         kernel_printf("FS_WRITE( len = %u ) pre-invocation", *pLen);
  77.                     }
  78.  
  79.                     /*
  80.                      * Locks the user buffer to check read access and to prevent other threads from freing it
  81.                      * behind us when we are sleeping. (this is a long term verify lock)
  82.                      */
  83.                     if (Read_Write) {
  84.                         rc = DevHlp32_VMLock(VMDHL_LONG | VMDHL_VERIFY, pData, *pLen, (void *)-1, __StackToFlat(lock), __StackToFlat(&PgCount));
  85.                         if ((rc == NO_ERROR) || (rc == ERROR_NOBLOCK)) {
  86.                             /*
  87.                              * Gets the file structure from psffsd
  88.                              */
  89.                             if ((psffsd->f) && (psffsd->f->f_magic == FILE_MAGIC)) {
  90.  
  91.                                 /*
  92.                                  * Tests if it is a regular file
  93.                                  */
  94.                                 if (S_ISREG(psffsd->f->f_inode->i_mode)) {
  95.  
  96.                                     /*
  97.                                      * If the file position in psffsi is not consistent with the
  98.                                      * one in the file structure in psffsd, something went wrong => panic
  99.                                      */
  100.                                     if (psffsi->sfi_position == (unsigned long)psffsd->f->f_pos) {
  101.                                     /*
  102.                                      * Now we do the actual write operation
  103.                                      */
  104.                                     err   = VFS_write(psffsd->f, pData, (loff_t)*pLen, __StackToFlat(&BytesWritten));
  105.                                     *pLen = (unsigned short)BytesWritten;
  106.                                     psffsi->sfi_tstamp  |= ST_PWRITE;
  107.                                     psffsi->sfi_size     = psffsd->f->f_inode->i_size;
  108.                                     psffsi->sfi_position = psffsd->f->f_pos;
  109.                                     rc = err;
  110.  
  111.                                 } else {
  112.                                     kernel_printf("FS_WRITE( %lu ) very weird : psffsi->sfi_position != psffsd->f->f_pos : %ld %ld", psffsd->f->f_inode->i_ino, psffsi->sfi_position, psffsd->f->f_pos);
  113.                                     *pLen = 0;
  114.                                     rc =  ERROR_WRITE_FAULT;        // Maybe we should FSH_INTERR ?
  115.                                 } /* sfi_position != f_pos */
  116.  
  117.                             } else {
  118.                                 kernel_printf("Can't FS_WRITE( %lu ) - Not a regular file", psffsd->f->f_inode->i_ino);
  119.                                 *pLen = 0;
  120.                                 rc = ERROR_ACCESS_DENIED;
  121.                             } /* !S_ISREG */
  122.                         } else {
  123.                             kernel_printf("FS_WRITE() - psffsd->f is NULL");
  124.                             rc = ERROR_INVALID_PARAMETER;
  125.                         } /* psffsd->f = NULL */
  126.  
  127.  
  128.                         if ((rc2 = DevHlp32_VMUnlock(__StackToFlat(lock))) == NO_ERROR) {
  129.                            /*
  130.                             * Nothing else to do
  131.                             */
  132.                         } else {
  133.                             kernel_printf("FS_WRITE : VMUnlock() returned %d", rc2);
  134.                             rc = rc2;
  135.                         } /* VMUnlock failed */
  136.                     } else {
  137.                         kernel_printf("FS_WRITE : LockUserBuffer() returned %d", rc);
  138.                     } /* LockUserBuffer()  failed */
  139.                 } else {
  140.                     /*
  141.                      * ext2-os2.ifs in read only mode (-rw not set on the IFS command line)
  142.                      */
  143.                     kernel_printf("ERROR ! FS_WRITE() called and write access not enabled");
  144.                     rc = ERROR_WRITE_PROTECT;
  145.                 } /* Read_Write  = 0 */
  146.  
  147.                 if (trace_FS_WRITE) {
  148.                     kernel_printf("FS_WRITE( len = %u ) post-invocation (rc = %d)", *pLen, rc);
  149.                 }
  150.                 }
  151.             }
  152.         }
  153.     }
  154.     return rc;
  155. }
  156.